#copyright 2009 Kevin Daley
##This file is a part of Nyst.
#Nyst is free software: you can redistribute it and/or modify
 #   it under the terms of the GNU Lesser General Public License as published by
  #  the Free Software Foundation, either version 3 of the License, or
   # (at your option) any later version.

   # This program is distributed in the hope that it will be useful,
   # but WITHOUT ANY WARRANTY; without even the implied warranty of
   # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   # GNU Lesser General Public License for more details.

   # You should have received a copy of the GNU Lesser General Public License
   # along with this program.  If not, see <http://www.gnu.org/licenses/>.

#Kevin Daley, Lead Developer.  still.horse@gmail.com.

import xml.dom.minidom
import random
import engine
import cmath, numpy
class Natural(engine.NodeManager):
	def __init__(self, ID, number, cell_width, filename, attributes,sorted_lifeforms, cam=(50,50),(parents={}, children={}, offset, neighbors={}), (estring, cstrings, sstrings)):
		spectrum=["lattitude","elevation","depth","season","nearness to water", "precipitation volume rate", "precipitation hardness", "air temperature","land/water temperature","wind gradient x", "wind gradient y", "land/water gradient x", "land/water gradient y"]
		spectrum.extend(sorted_lifeforms)
		
		map.Environment.__init__(self, ID, number, cell_width, filename, (attributes.extend(xrange(len(spectrum))),spectrum), cam,(parents={}, children={}, offset, neighbors={}), (estring, cstrings, sstrings))
	
class Wikispecies:
	
	def __init__(self,species):
		self.doc = minidom.parse(species)
		self.species=self.doc.getElementsByTagName('title')
	def __call__(self):
		self.index=random.randint(0,len(species))
		name=self.species[index].firstChild.nodeValue
		if os.system("wget http://animaldiversity.ummz.umich.edu/site/accounts/information/"+name.replace(" ","_")+".html"):
		
			self.string = open(name.replace(" ","_")+".html").read()
			name_re=re.compile("""<div class="content header">

				<h1><em>"""+name+"""</em><br /><small style="font-weight: normal">.*</small></h1>


			</div>""").search(self.string)
		
			name_re.replace(("""<div class="content header">

			    <h1><em>"""+name+"""</em><br /><small style="font-weight: normal">""","")
			name_re.replace(("""</small></h1>


			</div>""","")
			gloss=re.compile("<br><a class=\"glossary\" onclick=\"glossPopup([0-9]*);return false\" href=\"http://animaldiversity.ummz.umich.edu/site/glossary/popup/[0-9]*.html\">[a-z]*</a>")
			gloss2=re.compile("(<br><a class=\"glossary\" onclick=\"glossPopup([0-9]*);return false\" href=\"http://animaldiversity.ummz.umich.edu/site/glossary/popup/[0-9]*.html\">)*(</a>)*")
			glossary=gloss.search(self.string).group()
			glossary=[i for i in glossary]
			for j in glossary:
				remove=gloss2.search(j).group()
				for i in xrange(len(remove)):
					j.replace(remove.pop(), " ")
			return ([name_re]).extend(glossary), 
		else:
			self.__call__()

class Species(engine.Entity):
		def cast_identity(self, name="...", wspecies=None, spectrum=None):
			self.name=name
			if wspecies!=None:
				wk=Wikispecies(wspecies)
				self.livable=wk()
				self.node.spectrum.extend(self.livable)
			if spectrum!=None:				
				self.livable=spectrum
				self.node.spectrum.extend(spectrum)
			else:
				return -1
		def islivable(self):
			for i in xrange(self.env.spectrum.index(self.livable[0]), len(self.livable)+self.env.spectrum.index(self.livable[0])):
				if self.env.spectrum[i]<.00001:
					self.kill()


		
		